# -*- coding:utf-8 -*-
#from mysite.models import Site
from django.db import models
from django.forms import ModelForm
import ltyx.settings
from django.conf import settings
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.contrib.sites.managers import CurrentSiteManager
import datetime



    


'''用户分组表'''
class Stage(models.Model):
    BAN_CHOICES = (
        ('1', '一班'),
        ('2', '二班'),
        ('3', '三班'),
        ('4', '四班'),
        ('5', '五班'),
        ('6', '六班'),
        ('7', '七班'),
        ('8', '八班'),
    )

    title=models.CharField('级单位',max_length=20,help_text='入学年份,如2012')
    ban=models.CharField('班',max_length=20,choices=BAN_CHOICES)
    employee = models.ManyToManyField('Employee',verbose_name='用户',null =True, blank=True,)
    photo = models.ManyToManyField('Photo',verbose_name='相册',null =True, blank=True,)
    class Meta:
        verbose_name = '级、班级管理'
        verbose_name_plural = '级、班级管理'
    def __unicode__(self):
        return self.ban





'''
用户表
nickname 昵称
employee_group 用户所在组
period 属于那届
届=当前年份-年级
'''

class Employee(models.Model):
    #自定义性别
    SEX_CHOICES = (
    ('N', '男'),
    ('V', '女'),
    )
    
    user = models.OneToOneField(User,verbose_name='帐号管理')
    sex = models.CharField('性别',max_length=10,null =True, blank=True,choices=SEX_CHOICES)
    birthday=models.DateTimeField('生日',null =True, blank=True,)
    address=models.CharField('地址',max_length=25,null =True, blank=True,)    
    phone = models.CharField('电话',max_length=20,null =True, blank=True,)
    ismaster = models.BooleanField('是否班主任',default=False)
    sort = models.CharField('学科',max_length=20,null =True, blank=True,)

    class Meta:
        verbose_name = '用户管理'
        verbose_name_plural = '用户管理'
        db_table = 'Employee' 
        #app_label = u"用户管理"  
        
    def __unicode__(self):
        return  self.user.username

    
'''主分类'''
class Type(models.Model):
    title = models.CharField('内容分类',max_length=100)
    view = models.BooleanField('是否首页显示',default=True)
    class Meta:
        verbose_name = '内容分类'
        verbose_name_plural = '内容分类'
    def __unicode__(self):
        return self.title
    
'''2级分类'''
class SubType(models.Model):
    title = models.CharField('内容子分类',max_length=100)
    summary =  models.CharField('栏目说明',max_length=250)
    Type = models.ForeignKey(Type,verbose_name=u'所属分类')
    class Meta:
        verbose_name = '内容子分类'
        verbose_name_plural = '内容子分类'
    def __unicode__(self):
        return self.title
  
class NewsComment(models.Model):
    article = models.ForeignKey('Article',verbose_name=u'所属文章')
    times=models.DateTimeField('发布时间',auto_now=True,)
    employee = models.OneToOneField(Employee,verbose_name=u'发布人')
    content=models.TextField('内容',null =True, blank=True,)
    status = models.BooleanField('审核',default=True)
    passuser = models.CharField('审核人',max_length=100,null =True, blank=True,)
    class Meta:
        verbose_name = '评论'
        verbose_name_plural = '评论'
    def __unicode__(self):
         return  self.employee.user.first_name + u'评论:'+self.content
    def save(self, * args, ** kwargs):
        '''
        if self.employee.user.nickname =='游客':
            self.status == False
        '''
        
        super(self.__class__, self).save(*args, ** kwargs)
  
'''
文章
blog_type 文章所在分组
comment 评论
times 文章提交时间
'''   
    
#图片上传位置
UPLOAD_ROOT = './upload'
class Article(models.Model):   

    subType = models.ForeignKey(SubType,verbose_name='分类')
    employee = models.ForeignKey(Employee,verbose_name='发布人',null =True, blank=True,)
    title = models.CharField('标题',max_length=20)
    times = models.DateTimeField('发布时间',auto_now=True)
    hits = models.IntegerField('点击量',null =True, blank=True,default=0)
    content = models.TextField('内容',null =True, blank=True,)
    
    class Meta:
        verbose_name = '文章管理'
        verbose_name_plural = '文章管理'
    def __unicode__(self):
        return  self.title



'''相册'''
class Photo(models.Model):
    title = models.CharField('相册名称',max_length=20)
    subType = models.ManyToManyField('Photos',verbose_name='相册',null =True, blank=True,)
    class Meta:
        verbose_name = '相册名称'
        verbose_name_plural = '相册名称'
    def __unicode__(self):
        return  self.title  


'''照片'''
class Photos(models.Model):
    
    title = models.CharField('照片名称',max_length=20)
    images = models.ImageField(upload_to = UPLOAD_ROOT, blank = True,verbose_name='照片')
    class Meta:
        verbose_name = '照片'
        verbose_name_plural = '照片'

    def __unicode__(self):
        return  self.title 



class StageComment(models.Model):
    stage = models.ForeignKey(Stage,verbose_name=u'所属班级')
    times=models.DateTimeField('发布时间',auto_now=True,)
    employee = models.ForeignKey(Employee,verbose_name=u'发布人')
    content=models.TextField('内容',null =True, blank=True,)
    status = models.BooleanField('审核',default=True)
    passuser = models.CharField('审核人',max_length=100,null =True, blank=True,)
    class Meta:
        verbose_name = '班级评论'
        verbose_name_plural = '班级评论'
    def __unicode__(self):
         return  self.stage
    def save(self, * args, ** kwargs):
        '''
        if self.employee.user.nickname =='游客':
            self.status == False
        '''
        super(self.__class__, self).save(*args, ** kwargs)



class BlogComment(models.Model):
    blog = models.ForeignKey('Blog',verbose_name=u'所属BLOG')
    times=models.DateTimeField('发布时间',auto_now=True,)
    employee = models.OneToOneField(Employee,verbose_name=u'发布人')
    content=models.TextField('内容',null =True, blank=True,)
    status = models.BooleanField('审核',default=True)
    passuser = models.CharField('审核人',max_length=100,null =True, blank=True,)
    class Meta:
        verbose_name = 'BLOG评论'
        verbose_name_plural = 'BLOG评论'
    def __unicode__(self):
         return  self.blog
    def save(self, * args, ** kwargs):
        '''
        if self.employee.user.nickname =='游客':
            self.status == False
        '''
        super(self.__class__, self).save(*args, ** kwargs)


'''
博客
photos：博文图片
'''   
class Blog(models.Model):
    TYPES_CHOICES = (
        ('Y', '原创'),
        ('Z', '转载'),
    )   

    author = models.ForeignKey(Employee,verbose_name='作者')
    title = models.CharField('标题',max_length=20,)
    times = models.DateTimeField('发布时间',auto_now=True)
    content = models.TextField('内容',null =True, blank=True,)
    photo=models.ManyToManyField(Photo,verbose_name='相册',null =True, blank=True,)
    types=models.CharField('类型',max_length=10,choices=TYPES_CHOICES,null =True, blank=True)
    class Meta:
        verbose_name = '博客管理'
        verbose_name_plural = '博客管理'
        #db_table = 'Blog' 
        #app_label = u"博客管理"  

    def __unicode__(self):
        return  self.title

    

'''
问题
user 回复用户
quizzer 提问者或者游客
question 提问者所提出的问题
reply 回复用户所回复的内容
hidden 是否隐藏
'''
class Question(models.Model):   

    user=models.OneToOneField(Employee,verbose_name="回复人")
    quizzer=models.CharField("提问者",max_length=20)
    question=models.TextField('问题',blank=True)
    reply=models.TextField("回复",blank=True,null =True,)
    times=models.DateTimeField('时间', auto_now=True,)
    hidden=models.BooleanField('是否隐藏',default=False,)
    class Meta:
        verbose_name = '问题管理'
        verbose_name_plural = '问题管理'
  

    def __unicode__(self):
        return  self.question



'''作业表'''
class Assignment(models.Model):

    teacher=models.CharField("教师",max_length=20)
    times=models.DateTimeField('布置时间',default=False,)
    task=models.TextField('作业内容',)
    stage=models.ForeignKey(Stage,verbose_name='班级')
    class Meta:
        verbose_name = '作业管理'
        verbose_name_plural = '作业管理'
    def __unicode__(self):
        return self.teacher
